define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dealer/index' + location.search,
                    add_url: 'dealer/add',
                    edit_url: 'dealer/edit',
                    del_url: 'dealer/del',
                    multi_url: 'dealer/multi',
                    table: 'dealer',
                }
            });

            var table = $("#table");

            // 修改表格搜索框的默认placeholder
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "经销商名";};

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                searchFormVisible: true,
                exportTypes: ['csv','excel'],
                clickToSelect: false,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {
                            field: 'shop',
                            title: __('Shop'),
                            operate: false,
                            formatter: function (value, row, index) {
                                return '<a href="shop" class="btn-addtabs" style="color: black">' + value + '</a>'
                            }
                        },
                        {
                            field: 'studio',
                            title: __('Studio'),
                            operate: false,
                            formatter: function (value, row, index) {
                                return '<a href="studio" class="btn-addtabs" style="color: black">' + value + '</a>'
                            }
                        },
                        {
                            field: 'users',
                            title: __('Users'),
                            operate: false,
                            formatter: function (value, row, index) {
                                return '<a href="consumer" class="btn-addtabs" style="color: black">' + value + '</a>'
                            }
                        },
                        {
                            field: 'orders',
                            title: __('Orders'),
                            operate: false,
                            formatter: function (value, row, index) {
                                return '<a href="order" class="btn-addtabs" style="color: black">' + value + '</a>'
                            }
                        },
                        {field: 'commissions', title: __('Commissions'), operate:false,
                            formatter: function (value, row, index,){
                                if(value !== '0.00'){
                                    return '<a href="commission" class="btn-addtabs" style="color: black">¥ ' + value + '</a>';
                                }else{
                                    return "0";
                                }
                            }
                        },
                        {field: 'distributions', title: __('Distributions'), operate:false,
                            formatter: function (value, row, index){
                                if(row.distributions){
                                    return "¥ " + row.distributions;
                                }else{
                                    return "0";
                                }
                            }
                        },
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'),
                            table: table,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: __('Detail'),
                                    classname: 'btn btn-success btn-dialog',
                                    url: 'dealer/detail',
                                },
                                {
                                    name: 'state',
                                    text: function (row) {
                                        if (row.status == 0){
                                            return __('Status 1');
                                        }else if (row.status == 1){
                                            return __('Status 0');
                                        }
                                    },
                                    // classname: 'btn btn-danger btn-ajax btn-restoreit',
                                    url: 'dealer/state',
                                    refresh: true,
                                },
                                {
                                    name: 'edit',
                                    text: __('Edit'),
                                    classname: 'btn btn-info btn-dialog',
                                    url: 'dealer/edit',
                                },
                                {
                                    name: 'del',
                                    text: __('Del'),
                                    classname: 'btn btn-primary btn-ajax',
                                    url: 'dealer/del',
                                    confirm: '确认删除?',
                                    refresh: true,
                                },
                            ],
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);

                                // 动态显示操作里按钮的颜色
                                if (row.status == 0){
                                    that.buttons[1].classname = 'btn btn-success btn-ajax btn-restoreit';
                                }else if (row.status == 1){
                                    that.buttons[1].classname = 'btn btn-danger btn-ajax btn-restoreit';
                                }

                                $(that.buttons[1]).click(function (){
                                    alert(111);
                                })

                                // 隐藏编辑和删除按钮
                                // $(table).data("operate-edit", null);
                                // $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            function test() {
                // 开始时间
                $('#c-stime-chose').click(function (){
                    if($(this).prop('checked')){
                        $('#c-stime').attr('disabled',false);
                        $('#c-stime').parent().css('display','block');
                    }
                })
                $('#c-stime-cancel').click(function (){
                    if($(this).prop('checked')){
                        $('#c-stime').attr('disabled',true);
                        $('#c-stime').parent().css('display','none');
                    }
                })

                // 结束时间
                $('#c-etime-chose').click(function (){
                    if($(this).prop('checked')){
                        $('#c-etime').attr('disabled',false);
                        $('#c-etime').parent().css('display','block');
                    }
                })
                $('#c-etime-cancel').click(function (){
                    if($(this).prop('checked')){
                        $('#c-etime').attr('disabled',true);
                        $('#c-etime').parent().css('display','none');
                    }
                })
            }
            Controller.api.bindevent(test());
        },
        edit: function () {
            function test2() {
                // 开始时间
                $('#c-stime-chose').click(function (){
                    if($(this).prop('checked')){
                        $('#c-stime').attr('disabled',false);
                        $('#c-stime').parent().css('display','block');
                    }
                });
                $('#c-stime-cancel').click(function (){
                    if($(this).prop('checked')){
                        $('#c-stime').attr('disabled',true);
                        $('#c-stime').parent().css('display','none');
                    }
                });

                // 结束时间
                $('#c-etime-chose').click(function (){
                    if($(this).prop('checked')){
                        $('#c-etime').attr('disabled',false);
                        $('#c-etime').parent().css('display','block');
                    }
                });
                $('#c-etime-cancel').click(function (){
                    if($(this).prop('checked')){
                        $('#c-etime').attr('disabled',true);
                        $('#c-etime').parent().css('display','none');
                    }
                });
            }

            Controller.api.bindevent(test2());
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});